﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace DwarfFortClone
{
    //TODO: Make Dwarf fortress
    class TileLayer
    {
        public string fileName;
        int tileWidth;
        public Texture2D tileTexture;
        public ContentManager content;
        public Dictionary<uint, TileType> tileTypes;
        public List<List<uint>> tileArray;
        public Tilemap tileMap;

        public TileLayer(ContentManager content, Tilemap tileMap, string filename, int tileWidth, List<List<uint> > tileArray)
        {
            this.content = content;
            this.fileName = filename;
            this.tileWidth = tileWidth;
            this.tileTypes = new Dictionary<uint, TileType>();
            this.tileArray = tileArray;
            this.tileMap = tileMap;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            for (int c = 0; c < tileArray[0].Count; c++)
            {
                for(int r = 0; r < tileArray.Count; r++)
                {
                    tileTypes[tileArray[r][c]].Draw(spriteBatch, new Vector2(c * tileWidth, r * tileWidth), Color.White);
                }
            }
        }

        public void LoadContent()
        {
            tileTexture = content.Load<Texture2D>(fileName);
            int numHorizTiles = tileTexture.Width / tileWidth;
            int numVertTiles = tileTexture.Height / tileWidth;
            uint id = 0;

            for (int c = 0; c < numHorizTiles; c++)
            {
                for (int r = 0; r < numVertTiles; r++)
                {
                    TileType newTile = new TileType(this, new Rectangle(c * tileWidth , r * tileWidth, tileWidth, tileWidth), id);
                    tileTypes[id] = newTile;
                    id++;
                }
            }
        }
    }
}
